//
//  MyScrollView.m
//  Merry
//
//  Created by 蒋杏飞 on 15/2/2.
//  Copyright (c) 2015年 蒋杏飞. All rights reserved.
//

#import "MyScrollView.h"
#import "UIImageView+WebCache.h"
#import "firstModel.h"
#import "headPictureViewController.h"
#import "MyImageView.h"
@interface MyScrollView ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSTimer *timer;

@end

@implementation MyScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.page = 1;
        
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        _scroll.bounces = NO;
        
        _scroll.delegate = self;
        [self addSubview:_scroll];
        
        self.array =[NSMutableArray array];
        
        self.dic =[NSMutableDictionary dictionary];
        self.array2 =[NSMutableArray array];
     
    }
    return self;
}
#pragma mark - 
#pragma mark 给scrollView添加图片
- (void)setImages:(NSMutableArray *)names firstModel:(firstModel*)Mdeol
{
    NSString *first = [names firstObject];
    NSString *last = [names lastObject];
    [names insertObject:last atIndex:0];
    [names addObject:first];
    [self.array2 addObject:names];
    [self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width * names.count, _scroll.frame.size.height)];
    [self.scroll setPagingEnabled:YES];
  
    int i = 0;
 
    for (firstModel *model in names)
   
    {
        MyImageView *imageView = [[MyImageView alloc] initWithFrame:CGRectMake(self.scroll.frame.size.width * i, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        
        imageView.userInteractionEnabled = YES;
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil];

        imageView.model =model;
       
        [self.scroll addSubview:imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        
        [imageView addGestureRecognizer:tap];
        
        i ++;
    }
    
    [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0)];
    
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.pa = [[UIPageControl alloc]initWithFrame:CGRectMake(120,130, 100,20)];
    _pa.numberOfPages = 6;
    [self addSubview:_pa];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)timer
{
    self.page++;
    CGFloat offWidth = self.scroll.frame.size.width * self.page;
    
    NSInteger num = self.scroll.contentSize.width / self.scroll.frame.size.width;
    
    [UIView animateWithDuration:0.8 animations:
     ^{
        
        [self.scroll setContentOffset:CGPointMake(offWidth, 0)];
    }];
    
    if (self.page == num - 1) {
        self.page = 1;
        [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0)];
    }
    self.pa.currentPage =self.page-1;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offPoint = scrollView.contentOffset;
    CGSize size = scrollView.contentSize;
    CGFloat width = scrollView.frame.size.width;
    
    self.page = offPoint.x / width;
    
    if (offPoint.x == size.width - width) {
        self.page = 1;
        [scrollView setContentOffset:CGPointMake(width, 0)];
         self.pa.currentPage =self.page;
    }
    if (offPoint.x == 0) {
        self.page = scrollView.contentSize.width / scrollView.frame.size.width - 2;
        
         self.pa.currentPage =self.page;
        
        [scrollView setContentOffset:CGPointMake(size.width - width * 2, 0)];
    }
}
#pragma mark - 
#pragma mark tapAction

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    MyImageView *image = (MyImageView *)tap.view;
    [self.delegate setimageView:image];
}

@end
